package com.dc.aimc.common.security.config;

import com.dc.aimc.common.redis.util.RedisUtil;
import com.dc.aimc.common.security.filter.TokenAuthFilter;
import com.dc.aimc.common.security.security.DefaultPasswordEncoder;
import com.dc.aimc.common.security.security.UnAuthEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

import java.util.List;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class TokenWebSecurityConfig extends WebSecurityConfigurerAdapter {

    private RedisUtil<String, List<String>> redisUtil;
    // @Autowired
    // private DefaultPasswordEncoder defaultPasswordEncoder;
    //
    // private UserDetailsService userDetailsService;


    /**
     * 配置设置
     * @param http
     * @throws Exception
     */
    //设置退出的地址和token，redis操作地址
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling()
                .authenticationEntryPoint(new UnAuthEntryPoint())//没有权限访问
                .and().csrf().disable()
                .authorizeRequests()

                // 测试放行hello
                .antMatchers("/**/hello").permitAll()
                .anyRequest().authenticated()
                // .and().logout().logoutUrl("/admin/acl/index/logout")//退出路径
                // .addLogoutHandler(new TokenLogoutHandler(tokenManager, redisTemplate)).and()
                // .addFilter(new TokenLoginFilter(authenticationManager(), tokenManager, redisTemplate))

                // 判断用户权限
                .and().addFilter(new TokenAuthFilter(authenticationManager(), redisUtil)).httpBasic();
    }

    //调用userDetailsService和密码处理
    // @Override
    // public void configure(AuthenticationManagerBuilder auth) throws Exception {
    //     auth.userDetailsService(userDetailsService).passwordEncoder(defaultPasswordEncoder);
    // }
}
